package com.cg.advice;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
public class ApiResult<T> {

    public static final Integer SUCCESS_CODE = 200;

    public static final Integer FAIL_CODE = 500;

    public static final String SUCCESS_MESSAGE = "success";

    public static final String FAIL_MESSAGE = "fail";

    /**
     * 返回状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    private ApiResult() {
    }

    public static <T> ApiResult<T> error(String msg){
        ApiResult<T> errorResult = new ApiResult<>();
        errorResult.setCode(FAIL_CODE);
        errorResult.setMsg(msg);
        return errorResult;
    }

    public static <T> ApiResult<T> success() {
        ApiResult<T> resultUtil = new ApiResult<>();
        resultUtil.setCode(SUCCESS_CODE);
        resultUtil.setMsg(SUCCESS_MESSAGE);
        return resultUtil;
    }

    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> resultUtil = success();
        resultUtil.setData(data);
        return resultUtil;
    }

    public static <T> ApiResult<T> success(String message, T data) {
        ApiResult<T> resultUtil = success();
        resultUtil.setMsg(message);
        resultUtil.setData(data);
        return resultUtil;
    }

    public static <T> ApiResult<T> success(Integer code, String message, T data) {
        ApiResult<T> resultUtil = new ApiResult<>();
        resultUtil.setCode(code);
        resultUtil.setMsg(message);
        resultUtil.setData(data);
        return resultUtil;
    }

    public static <T> ApiResult<T> fail() {
        ApiResult<T> resultUtil = new ApiResult<>();
        resultUtil.setCode(FAIL_CODE);
        resultUtil.setMsg(FAIL_MESSAGE);
        return resultUtil;
    }

    public static <T> ApiResult<T> fail(T data) {
        ApiResult<T> resultUtil = fail();
        resultUtil.setData(data);
        return resultUtil;
    }

    public static <T> ApiResult<T> fail(String message, T data) {
        ApiResult<T> resultUtil = fail();
        resultUtil.setMsg(message);
        resultUtil.setData(data);
        return resultUtil;
    }

    public static <T> ApiResult<T> fail(Integer code, String message) {
        ApiResult<T> resultUtil = fail();
        resultUtil.setCode(code);
        resultUtil.setMsg(message);
        return resultUtil;
    }

    public static <T> ApiResult<T> fail(Integer code, String message, T data) {
        ApiResult<T> resultUtil = new ApiResult<>();
        resultUtil.setCode(code);
        resultUtil.setMsg(message);
        resultUtil.setData(data);
        return resultUtil;
    }

}
